package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.AppException;
import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IMemberDao;
import com.yeshine.opendata.entity.Member;
import com.yeshine.opendata.service.IMemberService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by taohj on 2014/9/5.
 */
@Service
public class MemberServiceImpl extends ServiceBase<Member> implements IMemberService {


    @Autowired
    private IMemberDao dao;


    @Override
    public Result<Member> create(Member member) {

        if (dao.insert(member)>0){
            return  new Result<Member>("新增成功");
        }else{
            return  new Result<Member>(false,"新增失败");
        }
    }

    @Override
    public Result<Member> remove(Member member) {
        if (dao.delete(member)>0){
            return  new Result<Member>("删除成功");
        }else{
            return  new Result<Member>(false,"删除失败");
        }
    }

    @Override
    public Result<Member> modify(Member member) {
        if (dao.update(member)>0){
            return  new Result<Member>("更新成功");
        }else{
            return  new Result<Member>(false,"更新失败");
        }
    }

    @Override
    public Result<Member> getById(Integer id) {
        return new Result<Member>(dao.selectById(id));
    }

    @Override
    public Result<Member> getList(Member member) {
        return new Result<Member>(dao.selectList(member));
    }

    @Override
    public Result<Member> getByPage(PageResult<Member> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<Member>(pageResult);
    }

    @Override
    public Result<Member> create(List<Member> members) {
        for (Member m:members){
            Result<Member> result=this.create(m);
            if (!result.isSuccess()){
                throw new AppException(result.getMessage());
            }
        }
        return new Result<Member>("新增成功");
    }

    @Override
    public Result<Member> get(Member member) {
        return new Result<Member>(dao.select(member));
    }

    @Override
    public Result<Member> modify(List<Member> members) {
        for (Member m:members){
            Result<Member> result=this.modify(m);
            if (!result.isSuccess()){
                throw new AppException(result.getMessage());
            }
        }
        return new Result<Member>("更新成功");
    }

    @Override
    public Result<Member> remove(List<Member> members) {
        for (Member m:members){
            Result<Member> result=this.remove(m);
            if (!result.isSuccess()){
                throw new AppException(result.getMessage());
            }
        }
        return new Result<Member>("删除成功");
    }
}
